package com.zuxia.rabbit.consumer;

import com.rabbitmq.client.Channel;
import com.zuxia.dto.OrdersDto;
import com.zuxia.entity.Orders;
import com.zuxia.service.OrderService;
import com.zuxia.utils.TimeUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Component
public class OrderConsumer {
    @Resource
    private OrderService orderService;

    @RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "delay_queue"),
                    exchange = @Exchange(name = "error_ex",type = "direct"),
                    key = "order"
            )
    })
    public void recive(Map<String,String> map){
        System.out.println(map.get("ordersId"));
        System.out.println(map.get("group"));
        if(map.get("ordersId")!=null){
            Orders ordersById = orderService.getOrdersById(map.get("ordersId"));
            if(ordersById.getOrdersStatus()==0){
                orderService.upOrdersStatusById(map.get("ordersId"),2);
            }
        }else if(map.get("group")!=null){
            System.out.println("拼团单过期");
            System.out.println(map.get("group"));
        }
        System.out.println(TimeUtils.getY_M_D_H_M_S(System.currentTimeMillis()));
    }

    @RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = "delay_queue_group"),
                    exchange = @Exchange(name = "error_ex_group",type = "direct"),
                    key = "group"
            )
    })
    public void group(String id){
        try {
            boolean b = orderService.upOrderStatusById(id, 2);
            if(b){
                OrdersDto orderOnOrdersById = orderService.getOrderOnOrdersById(id, 1);
                List<com.zuxia.entity.order.Orders> orders = orderOnOrdersById.getOrders();
                //退款
                for(com.zuxia.entity.order.Orders order : orders){
                    orderService.upOrdersStatusById(order.getOrdersId(),3);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(TimeUtils.getY_M_D_H_M_S(System.currentTimeMillis()));
    }
}
